package com.windward.vii.web.controller;

import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.dao.entity.Device;
import com.windward.vii.domain.user.SignParam;
import com.windward.vii.service.SignService;
import com.windward.vii.service.aliyun.SmsService;
import com.windward.vii.utils.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by tiansj on 2017/8/13.
 */
@Controller
public class SignController {

    @Resource
    SignService signService;
    @Resource
    SmsService smsService;

    @RequestMapping(value = "v1/api0/sendSms", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sendSms(@RequestParam String mobile, HttpServletRequest request) {
        return smsService.sendSms(mobile, Utils.getRemoteHost(request));
    }

    @RequestMapping(value = "v1/api0/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult login(SignParam signParam, Device device, HttpServletRequest request) {
        if(signParam == null) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, ResultCode.PARAM_ERROR_MSG);
        }

        if(device == null) {
            device = new Device();
        }
        device.setRegisterIp(Utils.getRemoteHost(request));
        signParam.setDevice(device);

        return signService.login(signParam);
    }

    @RequestMapping(value = "v1/api1/logout", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult loginOut(HttpServletRequest request, @RequestHeader(value="token") String token) {
        return signService.logout(token);
    }

}
